package com.douhao.personal.controller;

import com.douhao.personal.model.Role;
import com.douhao.personal.service.RoleService;
import com.douhao.personal.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

    @Resource
    RoleService roleService;

    /**
     * 角色列表
     *
     * @return
     */
    @RequestMapping(value = "roleList")
    public ModelAndView index(String pageNo, Role role) {
        Page<Role> roleList = roleService.pageList(role, pageNo == null ? 1 : Integer.valueOf(pageNo), pageSize);
        ModelAndView modelAndView = new ModelAndView("permit/roleList", "page", roleList);
        modelAndView.addObject("role", role);
        return modelAndView;
    }

    /**
     * 角色删除
     * @param ids
     */
    @RequestMapping(value="deleteRoleById",method=RequestMethod.POST)
    public Map<String,Object> deleteRoleById(String ids){
        return roleService.deleteRoleById(ids);
    }

    /**
     * 角色添加
     * @param role
     * @return
     */
    @RequestMapping(value="addRole",method=RequestMethod.POST)
    public Map<String,Object> addRole(Role role){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            int count = roleService.insert(role);
            resultMap.put("status", 200);
            resultMap.put("successCount", count);
        } catch (Exception e) {
            resultMap.put("status", 500);
            resultMap.put("message", "添加失败，请刷新后再试！");
        }
        return resultMap;
    }



    /**
     * 查询用户的所属角色
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "findRoleByUserId", method = RequestMethod.GET)
    public Set<String> findRoleByUserId(String userId) {
        Set<String> roleByUserId = roleService.findRoleByUserId(userId);
        return roleByUserId;
    }

}
